package com.libs.xxf.simple.textactivity

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.LinearLayout
import com.libs.xxf.simple.R
import com.uuzuche.lib_zxing.activity.CaptureFragment
import com.uuzuche.lib_zxing.activity.CodeUtils

/**
 * TODO:
 * @author XXF
 * Create Time : 2018/2/5
 */
class ZxinDemo : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_zxin)
        val captureFragment = CaptureFragment()
        // 为二维码扫描界面设置定制化界面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera)

        captureFragment.analyzeCallback = analyzeCallback
        supportFragmentManager.beginTransaction().replace(R.id.fl_my_container, captureFragment).commit()

        initView()

    }

    var isOpen = false

    private fun initView() {
        val linearLayout = findViewById(R.id.linear1) as LinearLayout
        linearLayout.setOnClickListener {
            if (!isOpen) {
                CodeUtils.isLightEnable(true)
                isOpen = true
            } else {
                CodeUtils.isLightEnable(false)
                isOpen = false
            }
        }
    }

    /**
     * 二维码解析回调函数
     */
    internal var analyzeCallback: CodeUtils.AnalyzeCallback = object : CodeUtils.AnalyzeCallback {
        override fun onAnalyzeSuccess(mBitmap: Bitmap, result: String) {
            val resultIntent = Intent()
            val bundle = Bundle()
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS)
            bundle.putString(CodeUtils.RESULT_STRING, result)
            resultIntent.putExtras(bundle)
            this@ZxinDemo.setResult(RESULT_OK, resultIntent)
            this@ZxinDemo.finish()
        }

        override fun onAnalyzeFailed() {
            val resultIntent = Intent()
            val bundle = Bundle()
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED)
            bundle.putString(CodeUtils.RESULT_STRING, "")
            resultIntent.putExtras(bundle)
            this@ZxinDemo.setResult(RESULT_OK, resultIntent)
            this@ZxinDemo.finish()
        }
    }
}